package cn.hpclub.server.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.model.SysCodeType;
import cn.hpclub.server.model.SysCodeValue;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.validator.admin.SysCodeValueValidator;

public class SysCodeValueController extends BaseAdminController<SysCodeValue>{

	public void list(){
		ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
		setAttr("typelist", SysCodeType.dao.getCodeType());
		
        SysCodeValue code = new SysCodeValue();
        findByPageForSearch(code.getSelect(), code.getSqlExceptSelect(searchInfo));
        setAttrs(searchInfo);

        render("/admin/sys_code_value.tpl");
    }
	
	public void add(){
		setAttr("codeValueID", -1);
		setAttr("typelist", SysCodeType.dao.getCodeType());
		
        render("/admin/sys_code_value_input.tpl");
	}
	
	/**
     * @Title: save
     * @Description: 新增/编辑时的保存 (id为-1时为新增)
     */
	//@Before({SysCodeValueValidator.class})
    public void save(){
        int id = getParaToInt("codeValueID");

        if(-1 == id){
        	if(!isExist(getPara("valueCode"))){
        		addValueCode();
        	}else{
        		this.setAttr("errorMessage", "编码CODE重复，请重新输入编码");
                render("/common/error.tpl");
                return;
        	}
        } else{
            editValueCode();
        }

        redirect("/sysCodeValue/list");
    }

    /**
     * @Title: edit
     * @Description: 编辑
     */
    public void edit(){
    	setAttr("typelist", SysCodeType.dao.getCodeType());
    	
        String id = getPara("id");

        SysCodeValue code = SysCodeValue.dao.findById(id);

        setAttr("codeValueID", code.getInt("id"));
        setAttr("tid", code.getInt("type_id"));
        setAttr("valueName", code.getStr("name"));
        setAttr("valueCode", code.getStr("code"));
        setAttr("valueValue", code.getStr("value"));
        setAttr("valueSort", code.getInt("sort"));
        setAttr("is_deleted", code.getBoolean("is_deleted"));
        setAttr("valueRemark", code.getStr("remark"));

        render("/admin/sys_code_value_input.tpl");
    }

    /**
     * @Title: addValueCode
     * @Description:新增资源
     */
    @Before(Tx.class)
    public void addValueCode(){
    	SysCodeValue code = new SysCodeValue();

    	code.set("type_id", getPara("tid"));
        code.set("name", getPara("valueName"));
        code.set("code", getPara("valueCode"));
        code.set("value", getPara("valueValue"));
        code.set("sort", getPara("valueSort"));
        code.set("is_deleted", getParaToBoolean("is_deleted"));
        code.set("remark", getPara("valueRemark"));
        code.set("created_by", InterfaceUtil.getAdminId());
        code.set("created_time", new Date());

        code.save();
    }
    
    private boolean isExist(String code){
    	String sql = "select count(*) as cnt from sys_code_value where code = '" + code + "'";
    	Long cnt = Db.queryLong(sql);
    	
    	return (cnt > 0) ? true : false;
    }

    public void editValueCode(){
    	SysCodeValue code = new SysCodeValue();

        code.set("id", getPara("codeValueID"));
        code.set("type_id", getPara("tid"));
        code.set("name", getPara("valueName"));
        code.set("code", getPara("valueCode"));
        code.set("value", getPara("valueValue"));
        code.set("sort", getPara("valueSort"));
        code.set("is_deleted", getParaToBoolean("is_deleted"));
        code.set("remark", getPara("valueRemark"));
        code.set("last_updated_by", InterfaceUtil.getAdminId());
        code.set("last_updated_time", new Date());

        code.update();
    }

    /**
     * @Title: delete
     * @Description: 删除
     */
    @Before(Tx.class)
    public void delete() throws SQLException{
        String[] ids = getParaValues("ids");
        if(ids == null || ids.length <= 0){
            ajaxJsonErrorMessage("删除失败，无选中项");
        }

        boolean deleted = deleteCodeValueByIDs(ids);

        if(deleted){
            ajaxJsonSuccessMessage("删除成功！");
        } else{
            ajaxJsonErrorMessage("删除失败！");
            throw new SQLException("基础数据(小类)删除失败");
        }
    }

    private boolean deleteCodeValueByIDs(String[] ids){
        boolean deleted = true;
        for(String id : ids){
        	SysCodeValue code = new SysCodeValue();
            code.set("id", id);
            code.set("is_deleted", 1);
            code.set("last_updated_by", InterfaceUtil.getAdminId());
            code.set("last_updated_time", new Date());
            if(!code.update()){
                deleted = false;
                break;
            }
        }

        return deleted;
    }
    
}
